package com.ltu.timkor6.gamesite.games.missingbits.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.ltu.timkor6.gamesite.games.missingbits.shared.Question;
import com.ltu.timkor6.gamesite.shared.Result;
import com.ltu.timkor6.gamesite.users.client.Users;

public class MissingBits
{
	private MissingBitsComposite composite;
	
	private MissingBitsServiceAsync service = GWT.create(MissingBitsService.class);
	
	public MissingBits(Users users)
	{
		checkUser();
		composite = new MissingBitsComposite(this);
	}
	
	public void checkSubmit()
	{
		int questions = 0;
		boolean error = false;
		
		try
		{
			questions = Integer.parseInt(this.composite.getNumber());
		}
		catch(NumberFormatException e)
		{
			error = true;
			this.composite.addError("You must enter a number.");
		}
		
		if(error == false)
		{
			if(0 < questions && questions < 10)
			{
				composite.setElements(questions);
				start(questions);
			}
			else
			{
				this.composite.addError("This Hanoi game requires a number between 1 and 9.");
			}
		}
	}
	
	/**
	 * For user authentification
	 */
	public void checkUser()
	{
		// Initialize the service proxy.
		if(service == null)
		{
			service = GWT.create(MissingBitsService.class);
		}
		
		// Set up the callback object.
		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>()
		{
			public void onFailure(Throwable caught)
			{
				Window.alert("Error: " + caught.getMessage());
			}
			
			public void onSuccess(Boolean signedIn)
			{
			}
		};
		
		// Make the call to the stock price service.
		service.checkUser(callback);
	}
	
	/**
	 * Send start game.
	 *  - Defines number of questions to answer.
	 * @param questions
	 */
	public void start(int questions)
	{
		// Initialize the service proxy.
		if(service == null)
		{
			service = GWT.create(MissingBitsService.class);
		}
		
		// Set up the callback object.
		AsyncCallback<Question> callback = new AsyncCallback<Question>()
		{
			public void onFailure(Throwable caught)
			{
				Window.alert("Error: " + caught.getMessage());
			}
			
			public void onSuccess(Question question)
			{
				composite.question(question);
			}
		};
		
		// Make the call to the stock price service.
		service.start(questions, callback);
	}
	
	/**
	 * Send request for a new query.
	 */
	public void getQuestion()
	{
		// Initialize the service proxy.
		if(service == null)
		{
			service = GWT.create(MissingBitsService.class);
		}
		
		// Set up the callback object.
		AsyncCallback<Question> callback = new AsyncCallback<Question>()
		{
			public void onFailure(Throwable caught)
			{
				Window.alert("Error: " + caught.getMessage());
			}
			
			public void onSuccess(Question question)
			{
				composite.question(question);
			}
		};
		
		// Make the call to the stock price service.
		service.question(callback);
	}
	
	/**
	 * Send an answer to the server.
	 *  - The answer is sent back i String form.
	 * @param answer
	 */
	public void sendAnswer(String answer)
	{
		// Initialize the service proxy.
		if(service == null)
		{
			service = GWT.create(MissingBitsService.class);
		}
		
		// Set up the callback object.
		AsyncCallback<Integer> callback = new AsyncCallback<Integer>()
		{
			public void onFailure(Throwable caught)
			{
				Window.alert("Error: " + caught.getMessage());
			}
			
			public void onSuccess(Integer points)
			{
				composite.showNextQuestion(points);
			}
		};
		
		// Make the call to the stock price service.
		service.answer(answer, callback);
	}
	
	/**
	 * Send question to get results.
	 */
	public void getResult()
	{
		// Initialize the service proxy.
		if(service == null)
		{
			service = GWT.create(MissingBitsService.class);
		}
		
		// Set up the callback object.
		AsyncCallback<Result> callback = new AsyncCallback<Result>()
		{
			public void onFailure(Throwable caught)
			{
				Window.alert("Error: " + caught.getMessage());
			}
			
			public void onSuccess(Result result)
			{
				composite.showResult(result);
			}
		};
		
		// Make the call to the stock price service.
		service.result(callback);
	}
	
	/**
	 * Return the composite for this game.
	 * @return
	 */
	public Composite getComposite()
	{
		return composite;
	}
}
